<?php
class EventsController extends AppController {

	var $name = 'Events';
	var $uses = array('Event','EventUser','Comment');
	var $helpers = array('Avatar','Formater','Linker');
    
	function index() {
		$this->Event->recursive = 0;
		$this->set('events', $this->paginate());
	}
	function manage($event_id)
	{
		if (!$event_id) {
			$this->cakeError('not_found');
		}
		$event = $this->Event->findById($event_id);
		$event_user = $this->EventUser->find('first',array(
			'conditions'=>array(
				'EventUser.event_id'=>$event_id,
				'EventUser.user_id'=>$this->cuid,
				'EventUser.role'=>EventUser::sponsor
			)));
		if (!$event_user) {
			$this->cakeError('Access');
		}
		$this->set('event', $event);
	}
	
	function edit_avatar($event_id)
	{
		$this->set('title_for_layout', '设置活动图标');
		if (!$event_id) {
			$this->cakeError('not_found');
		}
		$event = $this->Event->findById($event_id);
		$event_user = $this->EventUser->find('first',array(
			'conditions'=>array(
				'EventUser.event_id'=>$event_id,
				'EventUser.user_id'=>$this->cuid,
				'EventUser.role'=>EventUser::sponsor
			)));
		if (!$event_user) {
			$this->cakeError('Access');
		}
		$this->set('event', $event);
	}
	
	function upload_avatar($event_id)
	{
		if (!event_id) {
			$this->cakeError('not_found');
		}
		if ($_post) {
			# code...
		}
		$upload_path = Event::avatar_path.'/'; #'a_path' 格式！！记得加 /

		if (isset($this->cuid)) {
			$dst_file_name = "e_{$this->cuid}";
		}else{
			$this->Session->cakeError('not_found');
		}
	    if(isset($_FILES['uploadfile'])){
           $file_type = $_FILES['uploadfile']['type'];	
           $file_size  = $_FILES['uploadfile']['size'];
           if (!in_array($file_type,$this->Thumbnail->allow_type_list)){
           		$this->Session->setFlash('只允许上传 gif jpeg png等图片格式的文件！');
           		$this->redirect('/events/upload_avatar');
           }
           if($file_size>$this->Thumbnail->max_file_size){
           		$this->Session->setFlash('上传图片不得超过500K');
           		$this->redirect('/events/upload_avatar');
           }
           
			$ext = end(explode('.',$_FILES['uploadfile']['name']));
			$tmp_name = time().'_'.'e_'.$this->cuid;
			$src_file="tmpimgs/".$tmp_name.'.'.$ext; 
            if(move_uploaded_file($_FILES['uploadfile']['tmp_name'],$src_file)){
                $src_img = $src_file;
                $result = $this->Thumbnail->createUserThumbs($src_img,$file_type,$dst_file_name,$upload_path);
                if ($result==true) {	
                	$this->Session->setFlash('头像上传成功！');
                	$this->User->id = $this->cuid;
                	$this->User->saveField('avatar',$dst_file_name.'.jpg');
                	$this->redirect('/settings');
                }else{
                	$this->Session->setFlash($result);
                	$this->redirect('/users/upload_avatar');
               }
            }else{
                echo "Failed to upload file Contact Site admin to fix the problem";
                exit;
            }
		}
	}

	function view($id = null) {
	    $this->paginate=array(
		    'order'=>'Comment.created desc'
        		);
		if (!$id) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Event->recursive=0;
		$event = $this->Event->read(null, $id);
		$role = $this->EventUser->find('first',array('conditions'=>array(
		    'EventUser.user_id'=>$this->cuid,
		    'EventUser.event_id'=>$id
		    )
    	));
    	$joiners = $this->User->find('all',array(
    	    'joins'=>array(
    	        array(
    	            'table'=>'event_users',
    	            'alias'=>'EventUser',
    	            'conditions'=>array(
    	                'EventUser.user_id = User.id',
        	                'EventUser.role'=>EventUser::join
    	                )
    	            )
    	        ),
    	    'conditions'=>array(
    	        'EventUser.event_id'=>$id
    	        )
    	    ));
    	$interesters = $this->User->find('all',array(
    	    'joins'=>array(
    	        array(
    	            'table'=>'event_users',
    	            'alias'=>'EventUser',
    	            'conditions'=>array(
    	                'EventUser.user_id = User.id',
        	                'EventUser.role'=>EventUser::interest
    	                )
    	            )
    	        ),
    	    'conditions'=>array(
    	        'EventUser.event_id'=>$id
    	        )
    	    ));
    	$sponsor = $this->User->find('first',array(
    	    'joins'=>array(
    	        array(
    	            'table'=>'event_users',
    	            'alias'=>'EventUser',
    	            'conditions'=>array(
    	                'EventUser.user_id = User.id',
        	                'EventUser.role'=>EventUser::sponsor
    	                )
    	            )
    	        ),
    	    'conditions'=>array(
    	        'EventUser.event_id'=>$id
    	        )
    	    ));
    	if (!empty($role)) {
    	    $role = $role['EventUser']['role'];
    	}
		$comments=$this->paginate('Comment',array('Comment.commentable_type'=>'Event','Comment.commentable_id'=>$id));
		$this->set('comments', $comments);    	
    	$this->set('sponsor', $sponsor);
    	$this->set('joiners', $joiners);
    	$this->set('interesters', $interesters);
		$this->set('event', $event);
		$this->set('role', $role);
	}
	
	function group($ggid = null){
		if ($ggid = null) {
			$this->redirect('/');
		}
		$this->paginate = array(
				'limit'=>20,
				'order'=> 'id desc'
					);
		$this->set('events', $this->paginate('Event',array('Event.group_id'=>$this->cgid)) );	
	}

	function add() {
		if (!empty($this->data)) {
			$this->Event->create();
			
			$this->data['Event']['begins']=$this->data['begin_date'].' '.$this->data['begin_time'];
			$this->data['Event']['ends']=$this->data['end_date'].' '.$this->data['end_time'];
			if ($this->Event->save($this->data)) {
			    $a_data = array(
			        'EventUser'=>array(
    			        'user_id'=>$this->cuid,
    			        'event_id'=>$this->Event->id,
        			    'role'=>EventUser::sponsor
			            )
			        );
			    $this->EventUser->create();
			    $this->EventUser->save($a_data);
				$this->Session->setFlash(__('活动创建成功！', true));
				$this->redirect("/events/view/{$this->Event->id}");
			} else {
				$this->Session->setFlash(__('活动创建失败，请稍后再试！', true));
			}
		}
	}
	function add_group_event() {
		if (!empty($this->data)) {
			$this->Event->create();
			if ($this->Event->save($this->data)) {
			    $a_data = array(
			        'EventUser'=>array(
    			        'user_id'=>$this->cuid,
    			        'event_id'=>$this->Event->id,
        			    'role'=>EventUser::sponsor
			            )
			        );
			    $this->EventUser->create();
			    $this->EventUser->save($a_data);
				$this->Session->setFlash(__('活动创建成功！', true));
				$this->redirect("/group/$this->cggid/");
			} else {
				$this->Session->setFlash(__('活动创建失败，请稍后再试！', true));
			}
		}
	}
	function join($id=null){
		if(!$id){
			$this->Session->setFlash("访问出错。");
			$this->redirect('/events');
		}
		$data = array(
			'EventUser'=>array(
				'user_id'=>$this->cuid,
				'event_id'=>$id,
					'role'=>EventUser::join
				)
			);
		$this->EventUser->create();
		if($this->EventUser->save($data)){
			$this->Session->setFlash("参与活动成功。");
			$this->redirect("/events/view/{$id}");
		}
	}
	function interest($id=null){
		if(!id){
			$this->Session->setFlash("访问出错。");
			$this->redirect('/events');
		}
		$data = array(
			'EventUser'=>array(
				'user_id'=>$this->cuid,
				'event_id'=>$id,
					'role'=>EventUser::join
				)
			);
		$this->EventUser->create();
		if($this->EventUser->save($date)){
			$this->Session->setFlash("参与活动成功。");
			$this->redirect("/events/view/{$id}");
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Event->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for event', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Event->delete($id)) {
			$this->Session->setFlash(__('Event deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Event was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>